import { createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
    path: "/",
    redirect: "/login"
  },
  {
    path: "/login",
    component: () => import("../Login.vue")
  },
  {
    path: "/home/jiance",
    component: () => import("../components/Equip.vue")
  },
  {
    path: "/training",
    component: () => import("../components/Training.vue")
  },
  {
    path: "/home",
    component: () => import("../Home.vue"),
    children: [
      {
        path: "/home",
        redirect: "/home/index"
      },
      {
        path: "/home/index",
        component: () => import("../components/Index.vue"),
        meta: {
          required: true
        }
      },
      {
        path: "/home/class1",
        component: () => import("../components/Class.vue"),
        meta: {
          required: true
        }
      },
      {
        path: "/home/growup",
        component: () => import("../components/GrowUp.vue"),
        meta: {
          required: true
        }
      },
      {
        path: "/home/mine",
        component: () => import("../components/Mine.vue"),
        meta: {
          required: true
        }
      },
      {
        path: "/home/equip",
        component: () => import("../components/Equip.vue")
      }
    ]
  },
  {
    path: "/home/liwu",
    component: () => import("../components/Liwu.vue")
  },
  {
    path: "/home/study",
    component: () => import("../components/Study.vue")
  },
  {
    path: "/home/wechat",
    component: () => import("../components/Wechat.vue")
  },
  {
    path: "/home/shezhi",
    component: () => import("../components/Shezhi.vue")
  },
  {
    path: "/home/class",
    component: () => import("../components/KeCheng.vue")
  },
  {
    path: "/schedule",
    component: () => import("../components/Schedule.vue")
  },
  {
    path: "/Pass",
    component: () => import("../components/Pass.vue")
  },
  {
    path: "/Information",
    component: () => import("../components/Information.vue")
  },
  {
    path: "/Achievement",
    component: () => import("../components/Achievement.vue")
  },
  {
    path: "/home/gift",
    component: () => import("../components/gift.vue")
  },
  {
    path: "/clinic",
    component: () => import("../components/clinic/clinic.vue")
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

router.beforeEach((to, from, next) => {
  if (to.meta.required) {
    if (localStorage.getItem("user") || to.path === "/login") {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
